package com.cxrj.zhsq.serviceManage.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;

import com.cxrj.zhsq.serviceManage.dao.PropertyServiceMapper;
import com.cxrj.zhsq.serviceManage.dao.PropertyServiceTypeMapper;
import com.cxrj.zhsq.serviceManage.entity.PropertyServiceEntity;
import com.cxrj.zhsq.serviceManage.entity.PropertyServiceType;
import com.cxrj.zhsq.serviceManage.service.PropertyService;

@Service
public class PropertyServiceImpl implements PropertyService {

	@Resource
	private PropertyServiceMapper propertyServiceMapper;

	@Resource
	private PropertyServiceTypeMapper propertyServiceTypeMapper;

	@Override
	public Map<String, Object> insertServiceType(PropertyServiceType propertyServiceType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyServiceType.getId().equals("")) {
			propertyServiceType.setId(UUID.randomUUID().toString());
			propertyServiceType.setCreateTime(new Date());
			propertyServiceType.setStatue("1");
			c = propertyServiceTypeMapper.insertSelective(propertyServiceType);
		} else {
			c = propertyServiceTypeMapper.updateByPrimaryKeySelective(propertyServiceType);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteServiceTypePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyServiceTypeMapper.selectByCount(sqlMap) + "";
		List<PropertyServiceType> list = propertyServiceTypeMapper.selectPropertyServiceTypesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateServiceType(PropertyServiceType propertyServiceType) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyServiceTypeMapper.updateByPrimaryKeySelective(propertyServiceType);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteServiceTypeById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyServiceType propertyServiceType = propertyServiceTypeMapper.selectByPrimaryKey(id);
		resultMap.put("serviceType", propertyServiceType);
		return resultMap;
	}

	@Override
	public Map<String, Object> insertService(PropertyServiceEntity propertyServiceEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = 0;
		if (propertyServiceEntity.getId().equals("")) {
			propertyServiceEntity.setId(UUID.randomUUID().toString());
			propertyServiceEntity.setCreateTime(new Date());
			propertyServiceEntity.setStatus("1");
			c = propertyServiceMapper.insertSelective(propertyServiceEntity);
		} else {
			c = propertyServiceMapper.updateByPrimaryKeySelective(propertyServiceEntity);
		}
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteServicePage(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Map<String, Object> sqlMap = new HashMap<String, Object>();
		response.setContentType("text/html;charset=utf-8");
		// 数据起始位置
		String start = request.getParameter("offset");
		// 数据长度
		String length = request.getParameter("limit");
		sqlMap.put("start", Integer.parseInt(start));
		sqlMap.put("length", Integer.parseInt(length));
		sqlMap.put("communityId", request.getSession().getAttribute("communityId"));
		// 总记录数
		String recordsTotal = "0";
		// 获取数据库总记录数
		recordsTotal = propertyServiceMapper.selectByCount(sqlMap) + "";
		List<PropertyServiceEntity> list = propertyServiceMapper.selectPropertyServicesPage(sqlMap);

		resultMap.put("total", recordsTotal);
		resultMap.put("recordsFiltered", recordsTotal);
		resultMap.put("rows", list);

		return resultMap;
	}

	@Override
	public Map<String, Object> updateService(PropertyServiceEntity propertyServiceEntity) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		Integer c = propertyServiceMapper.updateByPrimaryKeySelective(propertyServiceEntity);
		resultMap.put("result", 0);
		if (c > 0) {
			resultMap.put("result", 1);
		}
		return resultMap;
	}

	@Override
	public Map<String, Object> seleteServiceById(String id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		PropertyServiceEntity propertyServiceEntity = propertyServiceMapper.selectByPrimaryKey(id);
		resultMap.put("service", propertyServiceEntity);
		return resultMap;
	}

	@Override
	public Map<String, Object> selectServiceTypeByAll() {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		List<PropertyServiceType> list = propertyServiceTypeMapper.selectPropertyServiceTypeByAll();
		resultMap.put("list", list);
		return resultMap;
	}
}
